//
//  RegisterViewController.m
//  Sydeny
//
//  Created by Rainbow on 8/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RegisterViewController.h"
#import "ServiceHelper.h"
#import "UtilHelper.h"
#import "ITSActivityIndicator.h"


@implementation RegisterViewController
@synthesize registerBgView;
@synthesize registerTableView;
@synthesize emailCell;
@synthesize nickNameCell;
@synthesize phoneCell;
@synthesize passwordCell;
@synthesize repasswordCell;
@synthesize emailText;
@synthesize nicknameText;
@synthesize phoneText;
@synthesize passwordText;
@synthesize repasswordText;
@synthesize registerScrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [registerBgView release];
    [registerTableView release];
    [emailCell release];
    [nickNameCell release];
    [phoneCell release];
    [passwordCell release];
    [repasswordCell release];
    [emailText release];
    [nicknameText release];
    [phoneText release];
    [passwordText release];
    [repasswordText release];
    [registerScrollView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"注册";
    // Do any additional setup after loading the view from its nib.
    
    UIButton *backBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(tapBackBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    self.navigationItem.leftBarButtonItem = backItem;
    
    registerBgView.layer.cornerRadius = 12;
    [emailText becomeFirstResponder];
    
    [registerScrollView setContentSize:CGSizeMake(300, 400)];
}

- (void)viewDidUnload
{
    [self setRegisterBgView:nil];
    [self setRegisterTableView:nil];
    [self setEmailCell:nil];
    [self setNickNameCell:nil];
    [self setPhoneCell:nil];
    [self setPasswordCell:nil];
    [self setRepasswordCell:nil];
    [self setEmailText:nil];
    [self setNicknameText:nil];
    [self setPhoneText:nil];
    [self setPasswordText:nil];
    [self setRepasswordText:nil];
    [self setRegisterScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return emailCell;
    }
    else if(indexPath.row == 1){
        return nickNameCell;
    }
    else if(indexPath.row == 2){
        return phoneCell;
    }
    else if(indexPath.row == 3){
        return passwordCell;
    }
    else{
        return repasswordCell;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 103) {
        [registerScrollView setContentOffset:CGPointMake(0, 100) animated:YES];
    }
    else if(textField.tag == 102)
    {
        [registerScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

- (IBAction)tapOnRegisterBtn:(id)sender {
    
    if (![passwordText.text isEqualToString:repasswordText.text]) {
        [[ITSActivityIndicator currentIndicator] displayFailed:@"两次输入的密码不一致！"];
        return;
    }
    NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease];
    [params setObject:emailText.text forKey:@"username"];
    [params setObject:passwordText.text forKey:@"password"];
    [params setObject:emailText.text forKey:@"email"];
    [params setObject:phoneText.text forKey:@"telephone"];
    [params setObject:nicknameText.text forKey:@"nickname"];
    
    [[ServiceHelper sharedService] requestServerForMethod:SydenyRequestRegister WithParameters:params AndCallBackScope:self AndSuccessSel:@"registerSuccess:" AndFailedSel:@"registerFailed:"];
}

- (void)registerSuccess:(NSObject *)result
{
    NSDictionary *dic = (NSDictionary *)result;
    NSString *code = [UtilHelper getStringValue:[dic objectForKey:@"success"]];
    if ([@"0" isEqualToString:code]) {
        [[ITSActivityIndicator currentIndicator] displayCompleted:@"注册成功！"];
    }
    else
    {
        [[ITSActivityIndicator currentIndicator] displayFailed:@"注册失败！"];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)registerFailed:(NSObject *)result
{
    
}

-(void)tapBackBtn
{
    [self dismissModalViewControllerAnimated:YES];
}


@end
